<?php
namespace Partner\Controller;

use Utuo\BackgroundController;

class ProductGoodsConfigController extends BackgroundController {

	private $id;
	
	private $sharing_method = array(
		'0' => '默认',
		'1' => '自定义'
	);
	
	public function _initialize() {
		$this->id = I("id", 0, "intval");
		$this->setControllerTitle("扩展配置");
		$this->setActionTitle(array(
				'index' => '扩展配置',
		), null, "id=".$this->id);
		$this->setActionNav("index");
	}

	public function indexAction() {
		$id = I('id', 0, 'intval');
		$productGoodsConfig = M("ProductGoodsConfig a")->join("__PRODUCT_GOODS__ b on a.id=b.id")->where(array('b.pid'=>$this->aid, 'a.id'=>$id))->find();
		if ( !$productGoodsConfig ) {
			$productGoods = M("ProductGoods")->where( array('id'=>$id, 'pid' => $this->aid) )->find() or $this->error("无效商品!");
			if ( $productGoods ) {
				$productGoodsConfig = array(
						'id' => $productGoods['id'],
						'cid' => $productGoods['cid'],
						'page_title' => $productGoods['name'],
						'page_keywords' => $productGoods['name'],
						'page_content' => "", );
				false !== M("ProductGoodsConfig")->add( $productGoodsConfig ) or $this->error( "初始数据失败!" );
			}
		}
		$productGoodsConfig['sharingMethodOptions'] = $this->getRadio("sharing_method", $this->sharing_method, $productGoodsConfig['sharing_method']);
		$productGoodsConfig['percent'] = explode( ':', $productGoodsConfig['percent'] );
		array_sum( $productGoodsConfig['percent'] ) or $productGoodsConfig['percent'][0] = 10;
		$this->assign("productGoodsConfig", $productGoodsConfig);
		$this->display();
	}

	public function saveAction() {
		$this->submitPost("ProductGoodsConfig");
	}

}